package com.homegoods.area.model;

import android.os.Parcel;
import android.os.Parcelable;
import android.text.TextUtils;
import android.util.Log;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.annotation.JSONField;
import com.homegoods.area.item.AreaItem;
import com.kedll.library.adapter.MultiTypeAdapter;
import com.kedll.library.mvp.BaseModel;

import java.util.List;

/**
 * 必须实现 BaseModel
 *
 * @author QiQi
 */
public class AreaModel extends BaseModel<AreaItem> implements Parcelable {
    /**
     *   * "name": "中国",
     *      * "sid": "fJ8UmXBJ3Bj5ZPVitL2fVSalnUmbxwKmpKzabCVIKFPMz1PZwSUOhNwwNgVbV0lk",
     *      * "scount": "25",
     *      * "rid": "R1",
     *      * "items
     */
    /**
     * ID
     */
    @JSONField(alternateNames = {"sid"})
    private String id;
    /**
     * 地区名
     */
    @JSONField(alternateNames = {"title", "name"})
    private String areaName;
    private String scount;
    private String rid;
    private String items;


    /**
     * 是否有下一级地区
     */
    private boolean haveChild;

    private boolean isSelected;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getAreaName() {
        return areaName;
    }

    public void setAreaName(String areaName) {
        this.areaName = areaName;
    }

    public String getScount() {
        return scount;
    }

    public void setScount(String scount) {
        this.scount = scount;
    }

    public String getRid() {
        return rid;
    }

    public void setRid(String rid) {
        this.rid = rid;
    }

    public String getItems() {
        return items;
    }

    public void setItems(String items) {
        this.items = items;
    }

    public boolean isHaveChild() {
        if (!TextUtils.isEmpty(this.items)) {
            List<AreaModel> list = JSON.parseArray(items, AreaModel.class);
            if (list != null && list.size() > 0) {
                return true;
            }
        }
        return false;
    }


    public boolean isSelected() {
        return isSelected;
    }

    public void setSelected(boolean selected) {
        isSelected = selected;
    }

    @Override
    public AreaItem createItem(MultiTypeAdapter adapter) {
        return new AreaItem(this, adapter);
    }

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(this.id);
        dest.writeString(this.areaName);
        dest.writeByte(this.haveChild ? (byte) 1 : (byte) 0);
        dest.writeByte(this.isSelected ? (byte) 1 : (byte) 0);
    }

    public AreaModel() {
    }

    protected AreaModel(Parcel in) {
        this.id = in.readString();
        this.areaName = in.readString();
        this.haveChild = in.readByte() != 0;
        this.isSelected = in.readByte() != 0;
    }

    public static final Parcelable.Creator<AreaModel> CREATOR = new Parcelable.Creator<AreaModel>() {
        @Override
        public AreaModel createFromParcel(Parcel source) {
            return new AreaModel(source);
        }

        @Override
        public AreaModel[] newArray(int size) {
            return new AreaModel[size];
        }
    };
}
